package cn.itsource.user.service.imp;

import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import cn.itsource.utils.JsonResult;
import cn.itsource.user.domain.User;
import cn.itsource.user.query.UserQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImp implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> selectAll() {
        try {

            JsonResult.success("查询用户数据成功！");
            return userMapper.selectAll();

        }catch (Exception e){
            e.printStackTrace();
            JsonResult.error();
            return null;
        }
    }

    @Override
    public void delete(Integer id) {
        try {
            userMapper.delete(id);
            JsonResult.success("删除用户数据成功！");

        }catch (Exception e){
            e.printStackTrace();
            JsonResult.error();
        }

    }

    @Override
    public void update(User user) {
        if (user.id == null) {
            userMapper.add(user);
            System.out.println("新增");
            JsonResult.success("新增用户数据成功！");
        }else {
            userMapper.update(user);
            System.out.println("修改");
            JsonResult.success("修改用户数据成功！");
        }

    }

    @Override
    public void deleteMore(Long[] ids) {
        userMapper.deleteMore(ids);
    }

    @Override
    public PageInfo<User> queryUser(UserQuery userQuery) {
        System.out.println(userMapper);
        // 设置分页参数
        PageHelper.startPage(userQuery.getCurrentPage(),userQuery.getPageSize());
        List<User> users = userMapper.queryUser(userQuery);
        System.out.println(users);
        return new PageInfo<>(users);
    }


}
